package com.example.redis6.controller;

import com.example.redis6.model.ProductDo;
import com.example.redis6.service.ProductService;
import com.example.redis6.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * description: VideoController <br>
 * date: 2021/7/22 13:41 <br>
 *
 * @author: ZhangYixin <br>
 * version: 1.0 <br>
 */
@RestController
@RequestMapping("api/v1/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @PostMapping("add")
    public JsonData add(@RequestBody ProductDo productDo){
        productDo.setCreateTime(new Date());
        int save = productService.save(productDo);
        return JsonData.buildSuccess(save);
    }

    @PostMapping("update")
    public JsonData update(@RequestBody ProductDo productDo){
        ProductDo i = productService.updateById(productDo);
        return JsonData.buildSuccess(i);
    }

    @GetMapping("del")
    public JsonData delete(int id){
        int i = productService.delById(id);
        return JsonData.buildSuccess(i);
    }

    @GetMapping("find")
    public JsonData find(int id){
        ProductDo productDo = productService.findById(id);
        return JsonData.buildSuccess(productDo);
    }

    @GetMapping("page")
    public JsonData page(int page,int size){
        Map<String, Object> pageMap = productService.page(page, size);
        return JsonData.buildSuccess(pageMap);
    }

}
